{% extends "base.html" %}
{% block content %}
<h1>Obviews by example... polls reviewed</h1>
<p>
  This example is inspired from <a
  href="http://docs.djangoproject.com/en/1.1/intro/tutorial01/"
  target="_blank">django tutorial</a> -of course- and <a
  href="http://code.google.com/p/django-polls/"
  target="_blank">django-polls</a> from James Pells who adds a couple
  of niceties I found fun.
</p>
<p>
  I will explain a bit the code in a few days... but you will have to
  consider this example as homework for now: an exercice to practice
  what has been shown in the previous steps. Anyhow, if you need a few
  less theory and a bit more show... you have the <a
  href="http://polls.obviews.com/">real polls on obviews</a>, with
  (as you guessed) real stuff like polls and choices and votes (and
  code too)...
</p>
<h2><a name="models"></a>Models</h2>
<div class="highlight"><pre>
<span class="k">class</span> <span class="nc">Poll</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">title</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">200</span><span class="p">)</span>
    <span class="n">slug</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">SlugField</span><span class="p">(</span><span class="n">unique</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">description</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TextField</span><span class="p">(</span><span class="n">blank</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">null</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">pub_date</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateTimeField</span><span class="p">(</span><span class="n">auto_now_add</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">state</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">choices</span><span class="o">=</span><span class="n">STATE_CHOICES</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="n">STATE_DEFAULT</span><span class="p">)</span>
    <span class="n">ip_address</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">IPAddressField</span><span class="p">()</span>
    <span class="n">user</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">User</span><span class="p">)</span>
    <span class="n">objects</span> <span class="o">=</span> <span class="n">PublishedManager</span><span class="p">()</span>

<span class="k">class</span> <span class="nc">Choice</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">poll</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Poll</span><span class="p">,</span> <span class="n">null</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">blank</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
    <span class="n">choice</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">200</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Vote</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">poll</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Poll</span><span class="p">)</span>
    <span class="n">choice</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Choice</span><span class="p">)</span>
    <span class="n">pub_date</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateTimeField</span><span class="p">(</span><span class="n">auto_now_add</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">user</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">User</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="s">&#39;polls_vote&#39;</span><span class="p">)</span>
    <span class="n">ip_address</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">IPAddressField</span><span class="p">()</span>
</pre></div>

<h2><a name="detail"></a>First View: Poll detail</h2>
<div class="highlight"><pre>
<span class="k">class</span> <span class="nc">ViewPoll</span><span class="p">(</span><span class="n">View</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&quot;polls/poll_view.html&quot;</span>

    <span class="k">def</span> <span class="nf">build_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">poll_id</span><span class="p">):</span>
        <span class="n">poll</span> <span class="o">=</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">Poll</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="n">poll_id</span><span class="p">)</span>
        <span class="n">all_votes</span> <span class="o">=</span> <span class="n">Vote</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">poll</span><span class="o">=</span><span class="n">poll</span><span class="p">)</span>
        <span class="n">my_vote</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">is_authenticated</span><span class="p">()</span> <span class="ow">and</span> <span class="n">all_votes</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">user</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="p">)</span> <span class="ow">or</span> <span class="bp">False</span>
        <span class="n">votes</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">choice</span> <span class="ow">in</span> <span class="n">poll</span><span class="o">.</span><span class="n">choice_set</span><span class="o">.</span><span class="n">all</span><span class="p">():</span>
            <span class="n">votes</span><span class="p">[</span><span class="n">choice</span><span class="o">.</span><span class="n">choice</span><span class="p">]</span> <span class="o">=</span> <span class="n">all_votes</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">choice</span><span class="o">=</span><span class="n">choice</span><span class="p">)</span><span class="o">.</span><span class="n">count</span><span class="p">()</span>
        <span class="k">return</span> <span class="p">{</span><span class="s">&#39;poll&#39;</span><span class="p">:</span> <span class="n">poll</span><span class="p">,</span>
                <span class="s">&#39;my_vote&#39;</span><span class="p">:</span> <span class="n">my_vote</span><span class="p">,</span>
                <span class="s">&#39;votes&#39;</span><span class="p">:</span> <span class="n">votes</span><span class="p">}</span>
</pre></div>

<h2><a name="listing"></a>Listing View</h2>
<div class="highlight"><pre>
<span class="k">class</span> <span class="nc">PaginatedView</span><span class="p">(</span><span class="n">View</span><span class="p">):</span>
    <span class="n">defaults</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;per_page&quot;</span><span class="p">:</span> <span class="mi">25</span><span class="p">}</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">PaginatedView</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">model</span>
        
    <span class="k">def</span> <span class="nf">build_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">page</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">order_by</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="n">per_page</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s">&quot;per_page&quot;</span><span class="p">]</span>
        <span class="c"># cast page as int. do not catch ValueError (forced by URL)</span>
        <span class="n">page</span> <span class="o">=</span> <span class="n">page</span> <span class="ow">and</span> <span class="nb">int</span><span class="p">(</span><span class="n">page</span><span class="p">)</span> <span class="ow">or</span> <span class="mi">1</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">objects</span><span class="p">,</span> <span class="s">&#39;published&#39;</span><span class="p">):</span>
            <span class="n">object_list</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">published</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">object_list</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
        <span class="c"># order. Again, value filtered by URL</span>
        <span class="k">if</span> <span class="n">order_by</span><span class="p">:</span>
            <span class="n">object_list</span> <span class="o">=</span> <span class="n">object_list</span><span class="o">.</span><span class="n">order_by</span><span class="p">(</span><span class="n">order_by</span><span class="p">)</span>
        <span class="n">paginator</span> <span class="o">=</span> <span class="n">Paginator</span><span class="p">(</span><span class="n">object_list</span><span class="p">,</span> <span class="n">per_page</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">objects</span> <span class="o">=</span> <span class="n">paginator</span><span class="o">.</span><span class="n">page</span><span class="p">(</span><span class="n">page</span><span class="p">)</span>
        <span class="k">except</span> <span class="p">(</span><span class="n">EmptyPage</span><span class="p">,</span> <span class="n">InvalidPage</span><span class="p">):</span>
            <span class="n">objects</span> <span class="o">=</span> <span class="n">paginator</span><span class="o">.</span><span class="n">page</span><span class="p">(</span><span class="n">paginator</span><span class="o">.</span><span class="n">num_pages</span><span class="p">)</span>
        <span class="c"># return built params</span>
        <span class="k">return</span> <span class="p">{</span><span class="s">&quot;objects&quot;</span><span class="p">:</span> <span class="n">objects</span><span class="p">,</span>
                <span class="s">&quot;sort_field&quot;</span><span class="p">:</span> <span class="n">order_by</span><span class="p">}</span>

<span class="k">class</span> <span class="nc">PaginatedPollView</span><span class="p">(</span><span class="n">PaginatedView</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&quot;polls/poll_list.html&quot;</span>
    <span class="n">defaults</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;base_url&quot;</span><span class="p">:</span> <span class="s">&quot;poll_list&quot;</span><span class="p">}</span>
</pre></div>

<h2><a name="adding"></a>Adding Form</h2>
<div class="highlight"><pre>
<span class="k">class</span> <span class="nc">CreatePoll</span><span class="p">(</span><span class="n">FormView</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&quot;polls/poll_create.html&quot;</span>
    <span class="n">login_required</span> <span class="o">=</span> <span class="bp">True</span>
    <span class="n">form_class</span> <span class="o">=</span> <span class="n">CreatePollFormClass</span>
    
    <span class="k">def</span> <span class="nf">initial_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="nb">vars</span><span class="p">):</span>
        <span class="n">poll_id</span> <span class="o">=</span> <span class="nb">vars</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;poll_id&quot;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">poll_id</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">{</span><span class="s">&quot;initial&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s">&#39;state&#39;</span><span class="p">:</span> <span class="n">STATE_DEFAULT</span><span class="p">}}</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">{</span><span class="s">&quot;instance&quot;</span><span class="p">:</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">Poll</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="n">poll_id</span><span class="p">)</span> <span class="p">}</span>

    <span class="k">def</span> <span class="nf">process_form</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="nb">vars</span><span class="p">):</span>
        <span class="n">poll</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">commit</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
        <span class="n">poll</span><span class="o">.</span><span class="n">user</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span>
        <span class="n">poll</span><span class="o">.</span><span class="n">ip_address</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">META</span><span class="p">[</span><span class="s">&#39;REMOTE_ADDR&#39;</span><span class="p">]</span>
        <span class="n">poll</span><span class="o">.</span><span class="n">slug</span> <span class="o">=</span> <span class="n">slugify</span><span class="p">(</span><span class="n">poll</span><span class="o">.</span><span class="n">title</span><span class="p">)</span>
        <span class="n">poll</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">poll</span>
        
    <span class="k">def</span> <span class="nf">on_success</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">result</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="nb">vars</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">result</span><span class="o">.</span><span class="n">state</span> <span class="o">!=</span> <span class="n">STATE_PUBLISHED</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">reverse</span><span class="p">(</span><span class="s">&quot;poll_posted&quot;</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">reverse</span><span class="p">(</span><span class="s">&quot;poll_modify&quot;</span><span class="p">,</span> <span class="n">kwargs</span><span class="o">=</span><span class="p">{</span><span class="s">&#39;poll_id&#39;</span><span class="p">:</span><span class="n">result</span><span class="o">.</span><span class="n">pk</span><span class="p">}))</span>
        
<span class="k">class</span> <span class="nc">PostedView</span><span class="p">(</span><span class="n">View</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&quot;polls/poll_posted.html&quot;</span>
    <span class="n">defaults</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;p&quot;</span><span class="p">:</span> <span class="s">&quot;posted&quot;</span><span class="p">}</span>
</pre></div>

<h2><a name="editing"></a>Editing Form and adding choices by ajax</h2>
<div class="highlight"><pre>
<span class="k">class</span> <span class="nc">ModifyPoll</span><span class="p">(</span><span class="n">FormView</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&quot;polls/poll_create.html&quot;</span>
    <span class="n">login_required</span> <span class="o">=</span> <span class="bp">True</span>
    <span class="n">form_class</span> <span class="o">=</span> <span class="n">ModifyPollFormClass</span>
    
    <span class="k">def</span> <span class="nf">build_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">poll_id</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">{</span><span class="s">&#39;poll&#39;</span><span class="p">:</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">Poll</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="n">poll_id</span><span class="p">)}</span>
    
    <span class="k">def</span> <span class="nf">initial_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="nb">vars</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">{</span><span class="s">&quot;instance&quot;</span><span class="p">:</span> <span class="nb">vars</span><span class="p">[</span><span class="s">&quot;poll&quot;</span><span class="p">]}</span>

    <span class="k">def</span> <span class="nf">process_form</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="nb">vars</span><span class="p">):</span>
        <span class="n">poll</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">poll</span>
        
    <span class="k">def</span> <span class="nf">on_success</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">result</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="nb">vars</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">result</span><span class="o">.</span><span class="n">get_absolute_url</span><span class="p">())</span>

<span class="k">class</span> <span class="nc">CreateChoice</span><span class="p">(</span><span class="n">AjaxFormView</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&quot;polls/choice_create.html&quot;</span>
    <span class="n">login_required</span> <span class="o">=</span> <span class="bp">True</span>
    <span class="n">form_class</span> <span class="o">=</span> <span class="n">CreateChoiceFormClass</span>

    <span class="k">def</span> <span class="nf">build_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">poll_id</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">{</span><span class="s">&#39;poll&#39;</span><span class="p">:</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">Poll</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="n">poll_id</span><span class="p">)}</span>
    
    <span class="k">def</span> <span class="nf">initial_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="nb">vars</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">{</span><span class="s">&quot;initial&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s">&#39;poll&#39;</span><span class="p">:</span> <span class="nb">vars</span><span class="p">[</span><span class="s">&#39;poll&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">pk</span><span class="p">}}</span>

    <span class="k">def</span> <span class="nf">process_form</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="nb">vars</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">form</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
        
    <span class="k">def</span> <span class="nf">ajax_success</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">result</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="nb">vars</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s">&#39;&lt;li&gt;</span><span class="si">%s</span><span class="s">&lt;/li&gt;&#39;</span><span class="o">%</span> <span class="n">result</span><span class="o">.</span><span class="n">choice</span><span class="p">)</span>
        
    <span class="k">def</span> <span class="nf">noajax_success</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">result</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="nb">vars</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">result</span><span class="o">.</span><span class="n">poll</span><span class="o">.</span><span class="n">get_absolute_url</span><span class="p">())</span>
</pre></div>

<h2><a name="deleting"></a>Deleting</h2>
<div class="highlight"><pre>
<span class="k">class</span> <span class="nc">DeletePoll</span><span class="p">(</span><span class="n">View</span><span class="p">):</span>
    <span class="n">login_required</span> <span class="o">=</span> <span class="bp">True</span>
    
    <span class="k">def</span> <span class="nf">build_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">poll_id</span><span class="p">):</span>
        <span class="n">poll</span> <span class="o">=</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">Poll</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="n">poll_id</span><span class="p">)</span>
        <span class="n">poll</span><span class="o">.</span><span class="n">delete</span><span class="p">()</span>
        <span class="k">raise</span> <span class="n">CutResponse</span><span class="p">(</span><span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">reverse</span><span class="p">(</span><span class="s">&#39;poll_home&#39;</span><span class="p">)))</span>
</pre></div>

<h2><a name="voting"></a>Voting</h2>
<div class="highlight"><pre>
<span class="k">class</span> <span class="nc">DoVote</span><span class="p">(</span><span class="n">View</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&quot;polls/poll_vote.html&quot;</span>
    <span class="n">login_required</span> <span class="o">=</span> <span class="bp">True</span>
    
    <span class="k">def</span> <span class="nf">build_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">poll_id</span><span class="p">,</span> <span class="n">choice_id</span><span class="p">):</span>
        <span class="n">user</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span>
        <span class="n">poll</span> <span class="o">=</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">Poll</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="n">poll_id</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">user</span><span class="o">.</span><span class="n">is_authenticated</span><span class="p">()</span> <span class="ow">and</span> <span class="n">Vote</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">poll</span><span class="o">=</span><span class="n">poll</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">user</span><span class="o">=</span><span class="n">user</span><span class="p">):</span>
            <span class="k">raise</span> <span class="n">CutResponse</span><span class="p">(</span><span class="n">HttpResponse</span><span class="p">(</span><span class="s">&#39;already voted&#39;</span><span class="p">))</span>
        <span class="n">choice</span> <span class="o">=</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">Choice</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="n">choice_id</span><span class="p">)</span>
        <span class="n">vote</span> <span class="o">=</span> <span class="n">Vote</span><span class="p">(</span><span class="n">user</span><span class="o">=</span><span class="n">user</span><span class="p">,</span> <span class="n">poll</span><span class="o">=</span><span class="n">poll</span><span class="p">,</span> <span class="n">choice</span><span class="o">=</span><span class="n">choice</span><span class="p">,</span>
                    <span class="n">ip_address</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">META</span><span class="p">[</span><span class="s">&#39;REMOTE_ADDR&#39;</span><span class="p">])</span>
        <span class="n">vote</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
        <span class="k">return</span> <span class="p">{</span><span class="s">&#39;redirect&#39;</span><span class="p">:</span> <span class="n">poll</span><span class="o">.</span><span class="n">get_absolute_url</span><span class="p">()}</span>
</pre></div>


<h2><a name="registration"></a>Registration</h2>
<div class="highlight"><pre>
<span class="k">class</span> <span class="nc">Register</span><span class="p">(</span><span class="n">FormView</span><span class="p">):</span>
    <span class="n">template_name</span><span class="o">=</span><span class="s">&quot;registration/register.html&quot;</span>
    <span class="n">form_class</span> <span class="o">=</span> <span class="n">UserCreationForm</span>
    
    <span class="k">def</span> <span class="nf">process_form</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="nb">vars</span><span class="p">):</span>
        <span class="n">username</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&#39;username&#39;</span><span class="p">]</span>
        <span class="n">password</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&#39;password1&#39;</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">User</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create_user</span><span class="p">(</span><span class="n">username</span><span class="p">,</span> <span class="n">username</span><span class="o">+</span><span class="s">u&quot;@nomail.com&quot;</span><span class="p">,</span> <span class="n">password</span><span class="p">)</span>
</pre></div>
{% endblock %}
